import { http } from "@/utils/http";

// 车位接口类型定义
export interface ParkingSpace {
  id: number; // 车位ID
  spaceNo: string; // 车位编号
  cellId?: number; // 小区ID
  cellName?: string; // 小区名称
  type: number; // 车位类型 (1: 产权车位, 2: 公共车位, 3: 租赁车位, 4: 访客车位, 5: 其他)
  status: number; // 车位状态 (1: 空置, 2: 自用, 3: 出租, 4: 出售, 5: 占用, 6: 报废)
  area?: number; // 车位面积
  chargeStandardCount?: number; // 收费标准个数
  chargeStandards?: string; // 收费标准描述
  prices?: string; // 价格列表，逗号分隔
  timesets?: string; // 时间设置列表，逗号分隔
  location?: string; // 具体位置
  remark?: string; // 备注
  createdTime?: string; // 创建时间
  updatedTime?: string; // 更新时间
  [key: string]: any;
}

// 车位详情接口类型定义
export interface ParkingSpaceDetail {
  id: number; // 车位ID
  spaceNo: string; // 车位编号
  cellName: string; // 小区名称
  ownerName?: string; // 所属业主名称
  type: number; // 车位类型
  status: number; // 车位状态
  area: number; // 车位面积
  chargeStandards: string; // 收费标准
  chargeStandardIds: string; // 收费标准ID数组（逗号分隔）
  boundVehicles: string; // 绑定车辆
  location: string; // 车位位置
  remark: string; // 备注
}

// 车位查询参数
export interface ParkingSpaceQueryParams {
  page?: number; // 页码
  limit?: number; // 每页数量
  spaceNo?: string; // 车位编号
  cellId?: number; // 小区ID
  type?: number; // 车位类型
  status?: number; // 车位状态
  tenantId?: number; // 租户ID (前端不传，后端自动添加)
}

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 车位列表响应
export interface ParkingSpaceListResponse
  extends ApiResponse<{
    list: ParkingSpace[];
    total: number;
    pageNum: number;
    pageSize: number;
    size: number;
    startRow: number;
    endRow: number;
    pages: number;
    prePage: number;
    nextPage: number;
    isFirstPage: boolean;
    isLastPage: boolean;
    hasPreviousPage: boolean;
    hasNextPage: boolean;
    navigatePages: number;
    navigatepageNums: number[];
    navigateFirstPage: number;
    navigateLastPage: number;
  }> { }

// 车位类型枚举
export const PARKING_SPACE_TYPE = {
  PROPERTY: 1, // 产权车位
  PUBLIC: 2, // 公共车位
  RENTAL: 3, // 租赁车位
  VISITOR: 4, // 访客车位
  OTHER: 5 // 其他
} as const;

// 车位状态枚举
export const PARKING_SPACE_STATUS = {
  VACANT: 1, // 空置
  SELF_USE: 2, // 自用
  RENT: 3, // 出租
  SALE: 4, // 出售
  OCCUPIED: 5, // 占用
  SCRAPPED: 6 // 报废
} as const;

// 车位类型标签映射
export const PARKING_SPACE_TYPE_LABELS = {
  [PARKING_SPACE_TYPE.PROPERTY]: "产权车位",
  [PARKING_SPACE_TYPE.PUBLIC]: "公共车位",
  [PARKING_SPACE_TYPE.RENTAL]: "租赁车位",
  [PARKING_SPACE_TYPE.VISITOR]: "访客车位",
  [PARKING_SPACE_TYPE.OTHER]: "其他"
};

// 车位状态标签映射
export const PARKING_SPACE_STATUS_LABELS = {
  [PARKING_SPACE_STATUS.VACANT]: "空置",
  [PARKING_SPACE_STATUS.SELF_USE]: "自用",
  [PARKING_SPACE_STATUS.RENT]: "出租",
  [PARKING_SPACE_STATUS.SALE]: "出售",
  [PARKING_SPACE_STATUS.OCCUPIED]: "占用",
  [PARKING_SPACE_STATUS.SCRAPPED]: "报废"
};

// 查询车位列表
export const getParkingSpaceList = (params?: ParkingSpaceQueryParams) => {
  return http.request<ParkingSpaceListResponse>("get", "/parkingSpace/list", {
    params
  });
};

// 通过ID查询车位详情
export const getParkingSpaceDetail = (id: number) => {
  return http.request<ApiResponse<ParkingSpaceDetail>>(
    "get",
    `/parkingSpace/detail?id=${id}`
  );
};

// 根据ID查询车位信息（用于编辑回显）
export const getParkingSpaceById = (id: number) => {
  return http.request<ApiResponse<ParkingSpace>>(
    "get",
    `/parkingSpace/findById?id=${id}`
  );
};

// 创建车位
export const createParkingSpace = (data?: ParkingSpace) => {
  return http.request<ApiResponse>("post", "/parkingSpace/add", { data });
};

// 更新车位
export const updateParkingSpace = (data?: ParkingSpace) => {
  return http.request<ApiResponse>("post", "/parkingSpace/update", { data });
};

// 删除车位
export const deleteParkingSpace = (id: number) => {
  return http.request<ApiResponse>("get", "/parkingSpace/delete", {
    params: { id }
  });
};

// 批量删除车位
export const batchDeleteParkingSpace = (ids: number[]) => {
  return http.request<ApiResponse>("post", "/parkingSpace/deleteBatch", {
    data: { ids }
  });
};

// 绑定车辆到车位
export interface BindVehicleRequest {
  parkingSpaceId: number; // 车位ID
  vehicleId: number; // 车辆ID
  bindType: number; // 绑定类型
  authorizeStart: string; // 授权开始时间
  authorizeEnd: string; // 授权结束时间
}

export const bindVehicleToParkingSpace = (data: BindVehicleRequest) => {
  return http.request<ApiResponse>("post", "/parkingSpace/bindVehicle", { data });
};

// 根据车牌号搜索车辆
export const searchVehicleByPlateNo = (plateNo: string) => {
  return http.request<ApiResponse<any[]>>("get", "/vehicle/search", {
    params: { plateNo },
  });
};

// 收费类型接口
export interface ChargeType {
  id: number;
  name: string;
  [key: string]: any;
}

// 收费项目接口
export interface ChargeItem {
  id: number;
  name: string;
  chargeTypeId: number;
  [key: string]: any;
}

// 收费标准接口
export interface ChargeStandard {
  id: number;
  name: string;
  chargeItemId: number;
  price: number; // 价格字段
  pricestr: string; // 价格字符串描述
  timeset: number; // 收费规格
  remarks: string; // 备注
  cellId: number;
  tenantId: number;
  createTime: string;
  updateTime: string;
  [key: string]: any;
}

// 绑定收费标准请求参数
export interface BindChargeStandardRequest {
  parkingSpaceId: number; // 车位ID
  chargeStandardId: number; // 收费标准ID
  amountOverride: number; // 覆盖金额
  isCurrent: number; // 是否当前标准
}

// 批量绑定收费标准请求参数
export interface BatchBindChargeStandardRequest {
  parkingSpaceIds: number[]; // 车位ID数组
  chargeStandardId: number; // 收费标准ID
  amountOverride: number; // 覆盖金额
  isCurrent: number; // 是否当前标准
}

// 查询所有收费类型
export const getAllChargeTypes = () => {
  return http.request<ApiResponse<ChargeType[]>>("get", "/spChargeType/findAll");
};

// 根据收费类型ID查询收费项目
export const getChargeItemsByTypeId = (chargeTypeId: number) => {
  return http.request<ApiResponse<ChargeItem[]>>("get", "/spChargeItem/findByChargeTypeId", {
    params: { id: chargeTypeId },
  });
};

// 根据收费项目ID查询收费标准
export const getChargeStandardsByItemId = (chargeItemId: number) => {
  return http.request<ApiResponse<ChargeStandard[]>>("get", "/SpChargeStandards/findByChargeItemId", {
    params: { id: chargeItemId },
  });
};

// 绑定收费标准到车位
export const bindChargeStandardToParkingSpace = (data: BindChargeStandardRequest) => {
  return http.request<ApiResponse>("post", "/parkingSpace/bindChargeStandard", { data });
};

// 批量绑定收费标准到车位
export const batchBindChargeStandardToParkingSpace = (data: BatchBindChargeStandardRequest) => {
  return http.request<ApiResponse>("post", "/parkingSpace/batchBindChargeStandard", { data });
};

// 根据ID查询收费标准
export const getChargeStandardById = (id: number) => {
  return http.request<ApiResponse<ChargeStandard>>("get", "/SpChargeStandards/findById", {
    params: { id },
  });
};

// 解除绑定收费标准
export const unbindChargeStandardFromParkingSpace = (data: { parkingSpaceId: number; chargeStandardId: number }) => {
  return http.request<ApiResponse>("post", "/parkingSpace/unbindChargeStandard", { data });
};

// 批量导入车位信息
export const importParkingSpaces = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return http.request<ApiResponse>("post", "/parkingSpace/import", {
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

// 导出车位Excel模板
export const exportParkingSpaceTemplate = () => {
  return http.request<Blob>("get", "/parkingSpace/exportTemplate", {
    responseType: 'blob',
  });
};

// 搜索车位
export const searchParkingSpaces = (spaceNo: string, cellId: number) => {
  return http.request<ApiResponse<ParkingSpace[]>>("get", "/parkingSpace/search", {
    params: { spaceNo, cellId },
  });
};

// 解绑车辆与车位
export const unbindVehicleFromParkingSpace = (data: { parkingSpaceId: number; vehicleId: number }) => {
  return http.request<ApiResponse>("post", "/parkingSpace/unbindVehicle", { data });
};

// 绑定住户到车位
export const bindOwnerToParkingSpace = (id: number, ownerId: number) => {
  return http.request<ApiResponse>("post", "/parkingSpace/bindOwner", {
    params: { id, ownerId }
  });
};

// 解绑住户与车位
export const unbindOwnerFromParkingSpace = (id: number) => {
  return http.request<ApiResponse>("post", "/parkingSpace/unbindOwner", {
    params: { id }
  });
};